﻿using System;
using System.Windows;
using System.Windows.Controls;

namespace Coding4Fun.Cannon.Phone.Controls
{
	public partial class SettingsView : UserControl
	{
		public SettingsView()
		{
			// Required to initialize variables
			InitializeComponent();

			// yes, i know should be done with databinding
			// was having issues, will fix later
			EndpointUrl.Text = App.ApplicationSettings.EndpointUrl;
			UseRcCar.IsChecked = App.ApplicationSettings.UseRcCar;
			IsInverseTilt.IsChecked = App.ApplicationSettings.IsInverseTilt;
		}

		public delegate void FinishedEventHandler(object sender, EventArgs e);
        public event FinishedEventHandler Finished;

		

		private void Save_Click(object sender, RoutedEventArgs e)
		{
			// save settings
			App.ApplicationSettings.EndpointUrl = EndpointUrl.Text;

			if (UseRcCar.IsChecked != null)
				App.ApplicationSettings.UseRcCar = UseRcCar.IsChecked.Value;

			if (IsInverseTilt.IsChecked != null)
				App.ApplicationSettings.IsInverseTilt = IsInverseTilt.IsChecked.Value;

			App.ApplicationSettings.Save();
			App.ReloadSettings();

			if (null != Finished)
				Dispatcher.BeginInvoke(Finished, this, new EventArgs());
		}

		private void Cancel_Click(object sender, RoutedEventArgs e)
		{
            if (null != Finished)
                Dispatcher.BeginInvoke(Finished, this, new EventArgs());
		}

	}
}